<template>
  <!-- 表单 -->
  <el-form ref="Form" label-width="120px" :model="formData" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formData.username" style="width:80%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="formData.mobile" style="width:80%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="formData.timeOfEntry" style="width:80%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <!-- 下拉菜单 -->
      <el-select v-model="formData.formOfEmployment" style="width:80%" placeholder="请选择">
        <el-option
          v-for="(item,ind) in options"
          :key="ind"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formData.workNumber" style="width:80%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="formData.departmentName" style="width:80%" placeholder="请选择部门" @focus="focusFn" />
      <div v-if="isShow" class="treebox">
        <!-- 树状组件 -->
        <el-tree :data="data" :props="{ label: 'name' }" @node-click="handleNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="formData.correctionTime" style="width:80%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button @click="closeFn">取消</el-button>
      <el-button type="primary" @click="submitFn">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
// 统一的请求文件
import { getDepartmentsApi, addEmployeeApi } from '@/api'
import moment from 'moment' // 引入moment第三方包
// 引入处理数组的函数
import { treeFn } from '@/utils/tree'
export default {
  props: {
    options: { // 父组件传递过来的聘用形式信息
      type: Array,
      required: true
    }
  },
  data() {
    return {
      formData: { // 表单双向绑定
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: { // 表单验证规则
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },
      data: [], // 树状组件渲染数据数组
      isShow: false // 控制树状组件的显示
    }
  },
  methods: {
    async getDepartments() { // 获取组织架构请求函数
      try {
        const res = await getDepartmentsApi()
        res.data.depts.shift()
        const result = treeFn(res.data.depts) // 请求回来的数据转换为树状数组
        this.data = result
        this.isShow = true // 显示部门树状组件
      } catch (err) {
        console.dir(err)
      }
    },
    async addEmployee() { // 新增员工请求函数
      const time = moment(this.formData.timeOfEntry).format('YYYY-MM-DD')
      console.log(time)
      this.formData.timeOfEntry = time
      try {
        await addEmployeeApi({ ...this.formData })
        this.$message({ // 给用户一个提示
          type: 'success',
          message: '添加员工成功!'
        })
      } catch (err) {
        console.dir(err)
        this.$message({ // 给用户一个提示
          type: 'danger',
          message: '添加员工失败!'
        })
      }
    },
    closeFn() { // 点击取消触发的事件
      this.$emit('close') // 触发父组件事件关闭弹窗
    },
    submitFn() { // 点击确定触发的事件
    // 兜底验证
      this.$refs.Form.validate(valid => {
        if (valid) {
          this.addEmployee()
          this.$emit('close') // 触发父组件事件关闭弹窗
        }
      })
    },
    focusFn() { // 新增员工部门表单获取焦点触发事件
      this.getDepartments()
    },
    handleNodeClick(data) { // 点击节点时触发的事件
      // 方法的参数可以获取到当前点击的节点信息,将其赋予双向绑定表单即可
      if (data.children === undefined) { // 判断如果有子部门就不做添加信息
        this.formData.departmentName = data.name
        this.isShow = false
      }
    }
  }
}
</script>

<style scoped>
.treebox{
  margin-top: 5px;
  /* position: absolute; */
  z-index: 99;
  width: 80%;
  overflow: hidden;
  border-radius: 5px;
  box-shadow: 4px 2px 5px 2px;
}
</style>
